<!-- 项目列表 -->
<template>
  <div class="app-container">
    <el-card class="box-card">
      <el-form :model="searchInfo" :inline="true" size="small">
        <el-form-item label="项目名称">
          <el-input v-model="searchInfo.name" placeholder="请输入项目名称" clearable />
        </el-form-item>
        <el-form-item label="科室">
          <el-select v-model="searchInfo.applyDeptId" clearable placeholder="请选择">
            <el-option v-for="item in deptList" :key="item.id" :label="item.depName" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="准入时间">
          <el-date-picker v-model="searchInfo.openTime" type="date" placeholder="选择日期" value-format="yyyy-MM-dd hh:mm:ss" />
        </el-form-item>
        <el-form-item label="申报人">
          <el-input v-model="searchInfo.applyUserName" clearable size="small" style="width: 180px" @keyup.enter.native="getList" />
        </el-form-item>
        <el-form-item label="负责人">
          <el-input v-model="searchInfo.applyManagerName" placeholder="请输入负责人" clearable size="small" style="width: 180px" @keyup.enter.native="getList" />
        </el-form-item>
        <el-form-item label="项目分类">
          <el-select v-model="searchInfo.projectClass" clearable placeholder="请选择">
            <el-option v-for="item in projectClassList" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="small" @click="onSearch">搜索</el-button>
          <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <div class="btn">
        <el-button v-permisaction="['add:HistoryProjectMaintenance']" type="primary" icon="el-icon-plus" size="small" @click="onAdd">新增</el-button>
      </div>
      <el-table v-loading="loading" :data="ticketList" stripe>
        <el-table-column label="#" type="index" width="80" align="center" />
        <el-table-column label="项目名称" prop="name" align="center" :show-overflow-tooltip="true" />
        <el-table-column label="项目分类" prop="projectClassName" align="center" :show-overflow-tooltip="true" />
        <el-table-column label="负责人姓名" prop="applyManagerName" align="center" />
        <el-table-column label="申报人" prop="applyUserName" align="center" />
        <el-table-column label="科室" prop="deptName" align="center" />
        <el-table-column label="准入时间" prop="fillTime" align="center" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="250">
          <template slot-scope="scope">
            <el-button v-permisaction="['detail:HistoryProjectMaintenance']" size="mini" type="text" icon="el-icon-edit" @click="handleView(scope.row)">查看详情</el-button>
            <el-button v-permisaction="['edit:HistoryProjectMaintenance']" size="mini" type="text" icon="el-icon-delete" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button v-permisaction="['delete:HistoryProjectMaintenance']" size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="pageInfo.total > 0" :total="pageInfo.total" :page.sync="pageInfo.currentPage" :limit.sync="pageInfo.pageSize" @pagination="onPagination" />
    </el-card>
    <ApplyDialog ref="applyDialog" @refreshTable="getList" />
    <ReviewDialog ref="reviewDialog" @refreshTable="getList" />
    <EditDialog ref="editDialog" @refreshTable="getList" />
  </div>
</template>

<script>
import {
  doQueryHisPage,
  doHisDelete
} from '@/api/xjs/work-order'
import { getNewDeptList } from '@/api/system/dept'
import ApplyDialog from './components/applyDialog.vue'
import ReviewDialog from './components/review.vue'
import EditDialog from './components/editDialog.vue'
// 搜索
import { parseTime } from '@/utils'
import { projectClassList } from '@/utils/common'
export default {
  name: 'MyDraft',
  components: { ApplyDialog, ReviewDialog, EditDialog },
  data() {
    return {
      searchInfo: {
        name: '', // 项目名称
        applyDeptId: '', // 科室id
        openTime: '', // 准入时间
        applyUserName: '', // 申报人
        applyManagerName: '', // 责任人
        projectClass: '' // 项目分类
      },
      deptList: [], // 科室列表
      searchInfoCopy: null, // 用于重置
      pageInfo: {
        currentPage: 1,
        pageSize: 10,
        total: 0
      },
      projectClassList: projectClassList, // 项目分类列表
      parseTime: parseTime, // 时间处理
      timeValue: [],
      loading: false, // loading加载
      ticketList: [], // 列表数据
      pickerOptions: {
        shortcuts: [
          {
            text: '最近一周',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
              picker.$emit('pick', [start, end])
            }
          }
        ]
      }
    }
  },
  computed: {},
  created() {
    this.searchInfoCopy = JSON.parse(JSON.stringify(this.searchInfo))
    this.getList()
    this.getDeptList()
  },
  methods: {
    // 新增
    onAdd() {
      // this.$refs.applyDialog.getNewDeptTreeList()
      this.$refs.applyDialog.open()
    },
    // 获取科室
    getDeptList() {
      getNewDeptList({ currentPage: 1, pageSize: 1000 }).then(res => {
        console.log(res)
        // const records = res.records[0]
        this.deptList = res.records
      })
    },
    // 搜索
    onSearch() {
      this.pageInfo.currentPage = 1
      this.getList()
    },
    // 重置
    resetQuery() {
      this.timeValue = []
      this.searchInfo = JSON.parse(JSON.stringify(this.searchInfoCopy))
      this.getList()
    },
    // 分页
    onPagination(obj) {
      this.pageInfo.currentPage = obj.page
      this.pageInfo.pageSize = obj.limit
      this.getList()
    },
    // 列表数据
    getList() {
      const params = {
        ...this.searchInfo,
        ...this.pageInfo
      }
      doQueryHisPage(params).then(res => {
        const data = res.records
        data.forEach(item => {
          const projectObj = this.projectClassList.find(it => it.value === item.projectClass)
          item.projectClassName = projectObj ? projectObj.label : ''
        })
        this.ticketList = data
        this.pageInfo.total = res.total
      })
    },
    // 表单数据弹窗
    handleView(row) {
      this.$refs.reviewDialog.open(row)
    },
    // 重新编辑提交弹窗
    handleEdit(row) {
      this.$refs.editDialog.open(row)
    },
    // 删除
    handleDelete(row) {
      this.$confirm('此操作将删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          doHisDelete({ id: row.id }).then(response => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.getList()
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    }
  }
}
</script>

<style scoped></style>
